#! /usr/bin/python3

import argparse
from multiprocessing import Pool
import time
import random

def argument():
    arg = argparse.ArgumentParser()
    arg.add_argument("--file", '-f', required = True, help = 'Fasta file name')
    arg.add_argument("--warning", '-w', default = False, type = bool, help = 'Print Warning information')
    arg.add_argument("--needmemsave", '-m', default = False, type = bool, help = 'Need memory save module, recommended >10000 sequences')
    return arg.parse_args()

if __name__ == '__main__':
    args = argument()
    f = open(args.file, "r")
    sequences = []
    __Warning__ = args.warning
    thisline = []
    for line in f:
        if line.startswith(">"):
            if thisline != []: sequences.append(''.join(thisline))
            thisline = []
        else:
            thisline.append(line.strip())
    if thisline != []:
        sequences.append(''.join(thisline))
    if len(sequences) < 1: 
        print("Error: this file has no sequences!!")
        exit(1)
    # Check the sequence is the same length
    seqlen1 = len(sequences[0])
    seqlen = [seqlen1]
    maxlen = seqlen1
    sequence_cnt = 1
    for i in range(1, len(sequences)):
        thislen = len(sequences[i])
        if thislen != seqlen1:
            print("Error: The length of sequence " + str(i) + "(" + str(thislen) + ") is not same as sequence 0(" + str(seqlen1) + ")!!")
            exit(1)
        seqlen.append(thislen)
        sequence_cnt += 1
    ans = 0
    false = False
    for i in range(seqlen1): # 第 i 个位置
        false = False
        if sequences[0][i] != '-': fir = sequences[0][i]
        else: fir = None
        for j in range(1, len(sequences)): # 第 j 条序列
            x = sequences[j][i]
            if x == '-': continue
            elif fir == None: 
                fir = x
                continue
            if(x != fir):
                false = True
                break
        if false == False:
            ans += 1
    print("TC pairs is", ans)
    print("TC score =", ans / seqlen1)